package com.nju.crawler.executor;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @authorliyunfei
 * @date2022/11/30
 **/
public abstract class AbstractCrawlGithubATask implements Runnable,Task{

    private static final String USER_INFO_URL = "https://api.github.com/users?repos%3E0&since=";// index
    private static final String REPO_INFO_URL = "https://api.github.com/users/username/repos";
    private static final String LIMIT_TOKEN = "token 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";
    private static final String SAVE_PATH_PREFIX = "E:\\JavaProjects\\CourseProjects\\cloud-compute-lab\\cloud-compute-crawler\\src\\main\\resources\\files";

    protected volatile boolean isStop = false;

    protected JSONArray requestUserInfo(int index){
        JSONArray rs;
        String json = HttpUtil.createGet(USER_INFO_URL+index)
                .header("Authorization",LIMIT_TOKEN)
                .execute()
                .body();
        if(json.contains("limit")){
            // 限流--，请切换token执行
        }
        rs = new JSONArray(json);
        return rs;
    }

    protected String request(String url){
              return HttpUtil.createGet(url)
                      .header("Authorization",LIMIT_TOKEN)
                      .execute()
                      .body();
    }

    @Override
    public void init() {
           //ignore
    }

    @Override
    public void execute() {
           //while (!isStop){
                 run();
           //}
    }

    @Override
    public void stop() {
           isStop = true;
    }
}
